
import { functionList } from './functions';
function setFormulaMode(CodeMirror) {
  const keywords = [];
  functionList.forEach(i => {
    i.children.forEach(j => {
      keywords.push(j.name);
    });
  });
  CodeMirror.defineMode('formula', function() {
    function t(t) {
      var e = {};
      for (var i = 0, a = t.length; i < a; ++i) e[t[i]] = true;
      return e;
    }
    var n = t(['false', 'true']);
    var s = t(keywords);
    var o = t(['MAP']);
    function i(t, e) {
      if (t.eatSpace()) return null;
      var i = t.next();
      if (i === '"' || i === "'") {
        r(t, i);
        return 'string';
      }
      if (/[\[\],\(\)]/.test(i)) return 'bracket';
      if (/[+\-*\/=<>!&|]/.test(i)) return 'operator';
      if (/\d/.test(i)) {
        t.eatWhile(/[\d\.]/);
        return 'number';
      }
      t.eatWhile(/[\w]/);
      var a = t.current();
      if (n.hasOwnProperty(a)) return 'atom';
      if (s.hasOwnProperty(a)) return 'keyword';
      if (o.hasOwnProperty(a)) return 'deprecate';
      return 'negative';
    }

    function r(t, e) {
      let i = false;
      let a;
      while ((a = t.next()) != null) {
        if (a === e && !i) return false;
        i = !i && a === '\\';
      }
      return i;
    }

    function a(t, e) {
      return (e.tokens[0] || i)(t, e);
    }
    return {
      startState: function() {
        return {
          tokens: []
        };
      },
      token: function(t, e) {
        return a(t, e);
      },
      fold: 'brace'
    };
  });
  CodeMirror.defineMIME('text/fx-formula', 'formula');
}

(function(mod) {
  mod(require('codemirror'));
})(function(CodeMirror) {
  setFormulaMode(CodeMirror);
});
